{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.metrics import r2_score\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CRIM</th>\n",
       "      <th>ZN</th>\n",
       "      <th>INDUS</th>\n",
       "      <th>CHAS</th>\n",
       "      <th>NOX</th>\n",
       "      <th>RM</th>\n",
       "      <th>AGE</th>\n",
       "      <th>DIS</th>\n",
       "      <th>TAX</th>\n",
       "      <th>PTRATIO</th>\n",
       "      <th>...</th>\n",
       "      <th>RAD_2</th>\n",
       "      <th>RAD_3</th>\n",
       "      <th>RAD_4</th>\n",
       "      <th>RAD_5</th>\n",
       "      <th>RAD_6</th>\n",
       "      <th>RAD_7</th>\n",
       "      <th>RAD_8</th>\n",
       "      <th>RAD_24</th>\n",
       "      <th>MEDV</th>\n",
       "      <th>log_MEDV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.18</td>\n",
       "      <td>0.067815</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.314815</td>\n",
       "      <td>0.577505</td>\n",
       "      <td>0.641607</td>\n",
       "      <td>0.269203</td>\n",
       "      <td>0.208015</td>\n",
       "      <td>0.3</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>3.218876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.242302</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.172840</td>\n",
       "      <td>0.547998</td>\n",
       "      <td>0.782698</td>\n",
       "      <td>0.348962</td>\n",
       "      <td>0.104962</td>\n",
       "      <td>0.5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>21.6</td>\n",
       "      <td>3.117950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.242302</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.172840</td>\n",
       "      <td>0.694386</td>\n",
       "      <td>0.599382</td>\n",
       "      <td>0.348962</td>\n",
       "      <td>0.104962</td>\n",
       "      <td>0.5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>34.7</td>\n",
       "      <td>3.575151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000293</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.063050</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.150206</td>\n",
       "      <td>0.658555</td>\n",
       "      <td>0.441813</td>\n",
       "      <td>0.448545</td>\n",
       "      <td>0.066794</td>\n",
       "      <td>0.6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>33.4</td>\n",
       "      <td>3.538057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000705</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.063050</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.150206</td>\n",
       "      <td>0.687105</td>\n",
       "      <td>0.528321</td>\n",
       "      <td>0.448545</td>\n",
       "      <td>0.066794</td>\n",
       "      <td>0.6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>36.2</td>\n",
       "      <td>3.616309</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       CRIM    ZN     INDUS  CHAS       NOX        RM       AGE       DIS  \\\n",
       "0  0.000000  0.18  0.067815   0.0  0.314815  0.577505  0.641607  0.269203   \n",
       "1  0.000236  0.00  0.242302   0.0  0.172840  0.547998  0.782698  0.348962   \n",
       "2  0.000236  0.00  0.242302   0.0  0.172840  0.694386  0.599382  0.348962   \n",
       "3  0.000293  0.00  0.063050   0.0  0.150206  0.658555  0.441813  0.448545   \n",
       "4  0.000705  0.00  0.063050   0.0  0.150206  0.687105  0.528321  0.448545   \n",
       "\n",
       "        TAX  PTRATIO    ...     RAD_2  RAD_3  RAD_4  RAD_5  RAD_6  RAD_7  \\\n",
       "0  0.208015      0.3    ...         0      0      0      0      0      0   \n",
       "1  0.104962      0.5    ...         1      0      0      0      0      0   \n",
       "2  0.104962      0.5    ...         1      0      0      0      0      0   \n",
       "3  0.066794      0.6    ...         0      1      0      0      0      0   \n",
       "4  0.066794      0.6    ...         0      1      0      0      0      0   \n",
       "\n",
       "   RAD_8  RAD_24  MEDV  log_MEDV  \n",
       "0      0       0  24.0  3.218876  \n",
       "1      0       0  21.6  3.117950  \n",
       "2      0       0  34.7  3.575151  \n",
       "3      0       0  33.4  3.538057  \n",
       "4      0       0  36.2  3.616309  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.read_csv('FE_boston-housing.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 506 entries, 0 to 505\n",
      "Data columns (total 23 columns):\n",
      "CRIM        506 non-null float64\n",
      "ZN          506 non-null float64\n",
      "INDUS       506 non-null float64\n",
      "CHAS        506 non-null float64\n",
      "NOX         506 non-null float64\n",
      "RM          506 non-null float64\n",
      "AGE         506 non-null float64\n",
      "DIS         506 non-null float64\n",
      "TAX         506 non-null float64\n",
      "PTRATIO     506 non-null float64\n",
      "B           506 non-null float64\n",
      "LSTAT       506 non-null float64\n",
      "RAD_1       506 non-null int64\n",
      "RAD_2       506 non-null int64\n",
      "RAD_3       506 non-null int64\n",
      "RAD_4       506 non-null int64\n",
      "RAD_5       506 non-null int64\n",
      "RAD_6       506 non-null int64\n",
      "RAD_7       506 non-null int64\n",
      "RAD_8       506 non-null int64\n",
      "RAD_24      506 non-null int64\n",
      "MEDV        506 non-null float64\n",
      "log_MEDV    506 non-null float64\n",
      "dtypes: float64(14), int64(9)\n",
      "memory usage: 91.0 KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#分离xy\n",
    "y=df['MEDV']\n",
    "x=df.drop(['MEDV','log_MEDV'],axis=1)\n",
    "feat_names=x.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(404, 21)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#建立训练集和测试集\n",
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=33,test_size=0.2)\n",
    "x_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>coef</th>\n",
       "      <th>columns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>20.356954</td>\n",
       "      <td>RM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.815739</td>\n",
       "      <td>ZN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>4.644938</td>\n",
       "      <td>RAD_24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>3.550457</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.684704</td>\n",
       "      <td>CHAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1.694896</td>\n",
       "      <td>RAD_7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1.353203</td>\n",
       "      <td>RAD_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1.235936</td>\n",
       "      <td>RAD_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.621810</td>\n",
       "      <td>INDUS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.055280</td>\n",
       "      <td>AGE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>-0.391399</td>\n",
       "      <td>RAD_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>-0.518265</td>\n",
       "      <td>RAD_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>-1.843667</td>\n",
       "      <td>RAD_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>-2.508106</td>\n",
       "      <td>RAD_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>-3.667536</td>\n",
       "      <td>RAD_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-5.109656</td>\n",
       "      <td>TAX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-6.819892</td>\n",
       "      <td>NOX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-8.455428</td>\n",
       "      <td>PTRATIO</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-9.984341</td>\n",
       "      <td>CRIM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-17.381223</td>\n",
       "      <td>DIS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-21.450356</td>\n",
       "      <td>LSTAT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         coef  columns\n",
       "5   20.356954       RM\n",
       "1    5.815739       ZN\n",
       "20   4.644938   RAD_24\n",
       "10   3.550457        B\n",
       "3    2.684704     CHAS\n",
       "18   1.694896    RAD_7\n",
       "19   1.353203    RAD_8\n",
       "14   1.235936    RAD_3\n",
       "2    0.621810    INDUS\n",
       "6   -0.055280      AGE\n",
       "15  -0.391399    RAD_4\n",
       "16  -0.518265    RAD_5\n",
       "13  -1.843667    RAD_2\n",
       "17  -2.508106    RAD_6\n",
       "12  -3.667536    RAD_1\n",
       "8   -5.109656      TAX\n",
       "4   -6.819892      NOX\n",
       "9   -8.455428  PTRATIO\n",
       "0   -9.984341     CRIM\n",
       "7  -17.381223      DIS\n",
       "11 -21.450356    LSTAT"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#线性回归\n",
    "from sklearn.linear_model import LinearRegression\n",
    "lr=LinearRegression()#建立线性回归模型\n",
    "lr.fit(x_train,y_train)#用训练集训练线性模型\n",
    "#对测试集进行预测\n",
    "y_test_pred_lr=lr.predict(x_test)\n",
    "y_train_pred_lr=lr.predict(x_train)\n",
    "#观察权重系数\n",
    "fs=pd.DataFrame({'columns':list(feat_names),'coef':list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the r2 score of LinearRegression on test is 0.6939789810509472\n",
      "the r2 score of LinearRegression on train is 0.7549146436868177\n"
     ]
    }
   ],
   "source": [
    "#使用r2_score评价模型\n",
    "print('the r2 score of LinearRegression on test is',r2_score(y_test,y_test_pred_lr))\n",
    "print('the r2 score of LinearRegression on train is',r2_score(y_train,y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAekAAAFsCAYAAADos0H8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAG09JREFUeJzt3XuUXWWZ5/HvQ1ISm0AwkGBIwAQEJVySYIHJIBCNiD1BgmtUYLiERQAXai/U8QaOTUnjtA40Ki3tkvYCCMqt1TC6XIMdSUcYG0xhGoGISQvRhBhCEEIQJJdn/jg7sahUpSpVp3LeOvX9rJVVZ++zz97Pe3ZO/erd+937RGYiSZLKs1ujC5AkSV0zpCVJKpQhLUlSoQxpSZIKZUhLklQoQ1qSpEIZ0hqSIuKRiJjZ6DpKExHvjojfR8SGiJi2C7e7ISIO6ua58yLi3jpt54mIeHs91iXtCoa0mk5Xv4g7/6LPzMMzc2EP65kYERkRwweo1BJdDXwoM0dm5i87P1m9Hy9UoboqIq6JiGH93Wi1vd/2dz1SszGkpQHSl3Av4A+C1wGP9LDMlMwcCZwInA6cP+BVSUOUIa0hqWNvOyKOjYjFEbE+ItZExDXVYouqn89WPccZEbFbRPzPiFgREU9FxE0RMapaz9ae97yI+B3w02r+udXy6yLiM5223RYRd0bEzRGxHjivqufnEfFsRKyOiK9ExKs61J4R8YGIWBYRz0fE30XEwdVr1kfE7R2X79TuLuuPiN0jYgMwDPiPiPjPnt7DzFwO3AdM7bD+URHxjaruVRFx5daedkS8PiL+LSKei4inI+K2Tm16ffV4n4i4q2rLA8DBHZbb7uhGRCyMiAuqxwdHxE+r9/rp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SK2zevJkFCxYwb948AO6++26WLVvGAw88QGZy6qmnsmjRItauXcv+++/Pj370IwCee+65V6xn9erVXH755bS3tzNq1Cje+ta3Mm3atB1ue8SIEXz/+99nr7324umnn2b69OmceuqprxiE9Z3vfIeTTz6ZT3/602zevJk//elPfWrnr3/9a+655x6ef/553vCGN3DxxRezfPlybrvtNu677z5aWlr4wAc+wC233MK5557L5z73OUaPHs3mzZuZNWsWDz30EEcdddS2uu+9994+1bEjhrQkCYAXX3yRqVOn8sQTT/CmN72Jk046CaiF9N13370tYDds2MCyZcs4/vjj+djHPsYnP/lJTjnlFI4//vhXrO/+++9n5syZjBlT+8Kn008/nd/85jc7rCEzueyyy1i0aBG77bYbq1atYs2aNbz2ta/dtswxxxzD+eefz8aNGznttNOYOnVqn9o7e/Zsdt99d3bffXfGjh3LmjVrWLBgAe3t7RxzzDHb3pOxY8cCcPvtt3P99dezadMmVq9ezaOPProtpE8//fQ+1dATD3dLkoC/nJNesWIFL7/88rZz0pnJpZdeypIlS1iyZAnLly9n3rx5HHroobS3t3PkkUdy6aWXcsUVV2y3zu4uQxo+fDhbtmzZtv6XX34ZgFtuuYW1a9fS3t7OkiVL2G+//ba71viEE05g0aJFjB8/nnPOOYebbrqpT+3dfffdtz0eNmwYmzZtIjOZO3futrY+9thjtLW18fjjj3P11VezYMECHnroIWbPnv2KuvbYY48+1dATe9LaKb0dcVrvkamSdp1Ro0Zx7bXXMmfOHC6++GJOPvlkPvOZz3DWWWcxcuRIVq1aRUtLC5s2bWL06NGcffbZjBw5khtuuOEV63nzm9/MJZdcwrp169hrr7244447mDJlClA7j9ve3s773vc+5s+fz8aNG4HaIfOxY8fS0tLCPffcw4oV23+r44oVKxg/fjwXXnghL7zwAg8++CDnnntuXdo+a9Ys5syZw0c+8hHGjh3LM888w/PPP8/69evZY489GDVqFGvWrOHHP/4xM2fOrMs2d8SQlqQCNfoP3WnTpjFlyhRuvfVWzjnnHJYuXcqMGTOA2iCvm2++meXLl/Pxj3+c3Xb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208>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#训练集上观察残差分布，是否是噪声为0的均值高斯噪声\n",
    "f,ax=plt.subplots(figsize=(7,5))\n",
    "f.tight_layout()\n",
    "ax.hist(y_train-y_train_pred_lr,bins=40,label='Residuals Linear',color='b',alpha=.5);\n",
    "ax.set_title('Historgram of Residuals')\n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# #残差分布和高斯分布相似，但是在>20处有一些取值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAARgAAADQCAYAAADcQn7hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJztnXuUVPWd4D/fLgqpJo+GkURpeY3xgA8GGDuIYU5WGSMkPsL4CDrGdSdOzOxqJro5GNyJAbKcSMJkdHeSMzkmOrrREYiYDmgy+AAnia6PJg1iq6xiNFJ4hARak+4OXXR/94+6t6muvvfWrcetW9X1/ZzTp+revnV/X6iub31fv+9XVBXDMIwoaIpbAMMwRi+mYAzDiAxTMIZhRIYpGMMwIsMUjGEYkWEKxjCMyDAFYxhGZJiCMQwjMkzBGIYRGWPiFiAMxx9/vE6fPj1uMQzDcNixY8dvVXVSoevqQsFMnz6djo6OuMUwDMNBRN4Mc525SIZhRIYpGMMwIqMuXCTDMKKlvTPNuq172N/dx+SWFMsXz2TpvNay72sKxjAanPbONLc8tJu+zAAA6e4+bnloN0DZSsZcJMNocNZt3TOkXFz6MgOs27qn7HubgjGMBmd/d19R54vBXCTDaCC8Yi0fTCXp7suMuPaDqWTZ65mCMYwGwS/WIni3zRUpf01TMIbRIPjFWvzo7h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md0A+o6s6gxVT1BeBbwM+d7nXrilzPiAlLUxtVRUT+FjhDVW+MWxYjesyCMQwjMsyCMQwjMsyCMQwjMkzBGIYRGaZgDMOIDFMwhmFEhikYwzAi4/8DRV9kQewoNMcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2823d3a85c0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#观察真值和预测值的散点图\n",
    "plt.figure(figsize=(4,3))\n",
    "plt.scatter(y_train,y_train_pred_lr)\n",
    "plt.plot([-3,3],[-3,3],'--k')\n",
    "plt.axis('tight')\n",
    "plt.xlabel('True price')\n",
    "plt.ylabel('Predict price')\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 真值后半段预测不佳，出现比较大的偏离值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the r2 score of RidgeCV on test is 0.6961684813070994\n",
      "the r2 score of RidgeCV on train is 0.7548524440445558\n"
     ]
    }
   ],
   "source": [
    "#岭回归/L2正则\n",
    "from sklearn.linear_model import RidgeCV\n",
    "#设置alpha范围\n",
    "alphas=[0.01,0.1,1,10,100]\n",
    "#或者使用此范围\n",
    "#n_alphas=20\n",
    "#alphas=np.logspace(-5,2,n_alphas)\n",
    "#生成岭回归模型\n",
    "ridge=RidgeCV(alphas=alphas,store_cv_values=True)\n",
    "#训练模型\n",
    "ridge.fit(x_train,y_train)\n",
    "#预测训练集\n",
    "y_test_pred_ridge=ridge.predict(x_test)\n",
    "y_train_pred_ridge=ridge.predict(x_train)\n",
    "#使用r2_score评估性能\n",
    "print('the r2 score of RidgeCV on test is',r2_score(y_test,y_test_pred_ridge))\n",
    "print('the r2 score of RidgeCV on train is',r2_score(y_train,y_train_pred_ridge))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
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PlLzjypSM/Wy0zmRWriERC+cZcB2M/srMDIY/7S7lwPbzOz1pHmygeKk958JugZvEkwbQKJbgGTlwJPB8KNAcgdix4cXkwgfgKbAr8xsKFAG9EtqX0Siqw+RUCkIJC6qekhMdQ+POUyiDyHMrBdwDzDS3Xeb2Yzj004huQ+Xo8FrGR/+3/saiT6ehpA4Qj+S1L5FUINIqHRqSOJiIXCjmTUOzttfSqJzuDdIPLilkZl1IfG4wuNWA32C4TOBg8DeoN01VaynEYneRwFuCZZfnbZAYXBE8jkSj588rh+Z0fusNHA6IpC4eJ7E+f/lJP5K/4a7bzOzZ4ErSOxw15F4MtTeYJ7ZJILhNXdfbmZLSfROuRH4WxXrOQgMNLPFwXJuPEVdvwaeNbMbSPQOejBp2seCGkRCpd5HJfbMrLUnnkrVkcRRwpggJFqS2DmPCa4tpLKsA+7euo7qWghMcPfddbE8karoiEAEZplZO6AZ8AN33wbg7ofN7H4Sz4Z9L50FBaevfq4QkHTQEYGISMzpYrGISMwpCEREYk5BICIScwoCEZGYUxCIiMScgkBEJOb+H7UtfChJre4lAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2823d505f98>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 0.1\n"
     ]
    }
   ],
   "source": [
    "#显示\n",
    "mse_mean=np.mean(ridge.cv_values_,axis=0)\n",
    "plt.plot(np.log10(alphas),mse_mean.reshape(len(alphas),1))\n",
    "#标出最佳参数位置\n",
    "plt.plot(np.log10(ridge.alpha_)*np.ones(3),[0.28,0.29,0.30])\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()\n",
    "print('alpha is:',ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>columns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>20.356954</td>\n",
       "      <td>20.209673</td>\n",
       "      <td>RM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.815739</td>\n",
       "      <td>5.668449</td>\n",
       "      <td>ZN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>4.644938</td>\n",
       "      <td>4.495999</td>\n",
       "      <td>RAD_24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>3.550457</td>\n",
       "      <td>3.569715</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.684704</td>\n",
       "      <td>2.703375</td>\n",
       "      <td>CHAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1.694896</td>\n",
       "      <td>1.652060</td>\n",
       "      <td>RAD_7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1.353203</td>\n",
       "      <td>1.369027</td>\n",
       "      <td>RAD_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1.235936</td>\n",
       "      <td>1.276021</td>\n",
       "      <td>RAD_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.621810</td>\n",
       "      <td>0.517086</td>\n",
       "      <td>INDUS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.055280</td>\n",
       "      <td>-0.048454</td>\n",
       "      <td>AGE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>-0.391399</td>\n",
       "      <td>-0.389052</td>\n",
       "      <td>RAD_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>-0.518265</td>\n",
       "      <td>-0.503234</td>\n",
       "      <td>RAD_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>-1.843667</td>\n",
       "      <td>-1.756881</td>\n",
       "      <td>RAD_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>-2.508106</td>\n",
       "      <td>-2.495217</td>\n",
       "      <td>RAD_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>-3.667536</td>\n",
       "      <td>-3.648724</td>\n",
       "      <td>RAD_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-5.109656</td>\n",
       "      <td>-4.958641</td>\n",
       "      <td>TAX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-6.819892</td>\n",
       "      <td>-6.565940</td>\n",
       "      <td>NOX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-8.455428</td>\n",
       "      <td>-8.431607</td>\n",
       "      <td>PTRATIO</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-9.984341</td>\n",
       "      <td>-9.567877</td>\n",
       "      <td>CRIM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-17.381223</td>\n",
       "      <td>-16.868997</td>\n",
       "      <td>DIS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-21.450356</td>\n",
       "      <td>-21.357121</td>\n",
       "      <td>LSTAT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      coef_lr  coef_ridge  columns\n",
       "5   20.356954   20.209673       RM\n",
       "1    5.815739    5.668449       ZN\n",
       "20   4.644938    4.495999   RAD_24\n",
       "10   3.550457    3.569715        B\n",
       "3    2.684704    2.703375     CHAS\n",
       "18   1.694896    1.652060    RAD_7\n",
       "19   1.353203    1.369027    RAD_8\n",
       "14   1.235936    1.276021    RAD_3\n",
       "2    0.621810    0.517086    INDUS\n",
       "6   -0.055280   -0.048454      AGE\n",
       "15  -0.391399   -0.389052    RAD_4\n",
       "16  -0.518265   -0.503234    RAD_5\n",
       "13  -1.843667   -1.756881    RAD_2\n",
       "17  -2.508106   -2.495217    RAD_6\n",
       "12  -3.667536   -3.648724    RAD_1\n",
       "8   -5.109656   -4.958641      TAX\n",
       "4   -6.819892   -6.565940      NOX\n",
       "9   -8.455428   -8.431607  PTRATIO\n",
       "0   -9.984341   -9.567877     CRIM\n",
       "7  -17.381223  -16.868997      DIS\n",
       "11 -21.450356  -21.357121    LSTAT"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算各特征权重\n",
    "fs=pd.DataFrame({'columns':list(feat_names),'coef_lr':list((lr.coef_.T)),'coef_ridge':list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.6945099371682091\n",
      "The r2 score of LassoCV on train is 0.7548861511225583\n"
     ]
    }
   ],
   "source": [
    "#L1正则/Lasso\n",
    "from sklearn.linear_model import LassoCV\n",
    "\n",
    "lasso=LassoCV()\n",
    "lasso.fit(x_train,y_train)\n",
    "\n",
    "y_test_pred_lasso=lasso.predict(x_test)\n",
    "y_train_pred_lasso=lasso.predict(x_train)\n",
    "\n",
    "print ('The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))\n",
    "print ('The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAELCAYAAADURYGZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzt3Xl4VPd97/H3VytoAQlJiEUGhAEDXjBG3pfEC66dzXZiN2mSliZOaZ+btkncPolz3Vs3TXtvnKTN0rTpdZOnpUkab7Gv7Sy2Md6axHYMtjE7YkcIJCEJCUlone/9Y46wTCQYbJ05s3xez6PnLPObOd8zA/OZs/2OuTsiIpK9cqIuQEREoqUgEBHJcgoCEZEspyAQEclyCgIRkSynIBARyXKhBoGZfcbMNprZJjP7bDBvipmtNrP6YFgeZg0iInJyoQWBmZ0D/BFwEbAEeJ+ZzQfuBNa4+3xgTTAtIiIRCXOLYBHwkrv3uPsg8DxwC3ATsCposwq4OcQaRETkFMIMgo3AVWZWYWZFwHuAM4Bqdz8IEAynhliDiIicQl5YL+zuW8zsHmA10AWsBwYTfb6ZrQRWAhQXFy9buHBhKHWKiGSqdevWHXb3qlO1s2T1NWRm/xtoAD4DvNvdD5rZdOA5dz/rZM+tq6vztWvXJqNMEZGMYWbr3L3uVO3CPmtoajCcBXwQ+DHwGLAiaLICeDTMGkRE5ORC2zUU+ImZVQADwKfdvd3MvgI8YGa3A/uA20KuQURETiLUIHD3K0eZ1wpcG+ZyRUQkcbqyWEQkyykIRESynIJARCTLKQhER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T07wZdUecFjy0m3gXKSDHg/mD8h8DDIx4bHl9HPHwA8oHvmNn5wBCwYET7ZuJdRYiESkEg2WKsm3+c7KYgx4j3DYOZ1QJ/CVzo7u1m9h/Dj53CyD5c+oLhEG/+3/sc8T6ClhDfQu8d0X5CUINIqLRrSLLFC8CHzSw32G9/FfFOv34JfCg4VlDNW7s13wLMC8YnAd1AR9DuxjGWk0O8V0mAjwavfzKTgYPBFsnvE7/d4bAFpGdvp5JmtEUg2eIR4vv/1xP/lf55dz9kZj8BriX+hbsdeJn4XaMgfg+MdwNPu/t6M3uNeC+ku4BfjbGcbuBsM1sXvM6HT1HXvwA/MbPbgGeD5w+7OqhBJFTqfVSynpmVuHuXmVUQ30q4PAiJicS/nC8Pji0k8lpd7l4yTnW9ANzk8ftxi4RGWwQi8FMzKwMKgC+7+yEAdz9mZncTv8/svmQWFOy++keFgCSDtghERLKcDhaLiGQ5BYGISJZTEIiIZDkFgYhIllMQiIhkOQWBiEiW+//Cw8uaG/eEWAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2823d6164e0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 0.0013884165768061959\n"
     ]
    }
   ],
   "source": [
    "mses=np.mean(lasso.mse_path_,axis=1)\n",
    "plt.plot(np.log10(lasso.alphas_),mses)\n",
    "#标出最佳参数位置\n",
    "#plt.plot(np.log10(lasso.alpha_)*np.ones(3),[0.3,0.4,1.0])\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()\n",
    "print('alpha is:',lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>coef_lasso</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>columns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>20.349884</td>\n",
       "      <td>20.356954</td>\n",
       "      <td>20.209673</td>\n",
       "      <td>RM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.690495</td>\n",
       "      <td>5.815739</td>\n",
       "      <td>5.668449</td>\n",
       "      <td>ZN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>4.849636</td>\n",
       "      <td>4.644938</td>\n",
       "      <td>4.495999</td>\n",
       "      <td>RAD_24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>3.527686</td>\n",
       "      <td>3.550457</td>\n",
       "      <td>3.569715</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.693696</td>\n",
       "      <td>2.684704</td>\n",
       "      <td>2.703375</td>\n",
       "      <td>CHAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2.018658</td>\n",
       "      <td>1.694896</td>\n",
       "      <td>1.652060</td>\n",
       "      <td>RAD_7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1.697906</td>\n",
       "      <td>1.353203</td>\n",
       "      <td>1.369027</td>\n",
       "      <td>RAD_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1.631360</td>\n",
       "      <td>1.235936</td>\n",
       "      <td>1.276021</td>\n",
       "      <td>RAD_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.422357</td>\n",
       "      <td>0.621810</td>\n",
       "      <td>0.517086</td>\n",
       "      <td>INDUS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.024262</td>\n",
       "      <td>-0.055280</td>\n",
       "      <td>-0.048454</td>\n",
       "      <td>AGE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.391399</td>\n",
       "      <td>-0.389052</td>\n",
       "      <td>RAD_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>-0.126658</td>\n",
       "      <td>-0.518265</td>\n",
       "      <td>-0.503234</td>\n",
       "      <td>RAD_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>-1.365961</td>\n",
       "      <td>-1.843667</td>\n",
       "      <td>-1.756881</td>\n",
       "      <td>RAD_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>-2.113486</td>\n",
       "      <td>-2.508106</td>\n",
       "      <td>-2.495217</td>\n",
       "      <td>RAD_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>-3.236791</td>\n",
       "      <td>-3.667536</td>\n",
       "      <td>-3.648724</td>\n",
       "      <td>RAD_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-4.872897</td>\n",
       "      <td>-5.109656</td>\n",
       "      <td>-4.958641</td>\n",
       "      <td>TAX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-6.632717</td>\n",
       "      <td>-6.819892</td>\n",
       "      <td>-6.565940</td>\n",
       "      <td>NOX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-8.400752</td>\n",
       "      <td>-8.455428</td>\n",
       "      <td>-8.431607</td>\n",
       "      <td>PTRATIO</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-9.708141</td>\n",
       "      <td>-9.984341</td>\n",
       "      <td>-9.567877</td>\n",
       "      <td>CRIM</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-17.122870</td>\n",
       "      <td>-17.381223</td>\n",
       "      <td>-16.868997</td>\n",
       "      <td>DIS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-21.473748</td>\n",
       "      <td>-21.450356</td>\n",
       "      <td>-21.357121</td>\n",
       "      <td>LSTAT</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    coef_lasso    coef_lr  coef_ridge  columns\n",
       "5    20.349884  20.356954   20.209673       RM\n",
       "1     5.690495   5.815739    5.668449       ZN\n",
       "20    4.849636   4.644938    4.495999   RAD_24\n",
       "10    3.527686   3.550457    3.569715        B\n",
       "3     2.693696   2.684704    2.703375     CHAS\n",
       "18    2.018658   1.694896    1.652060    RAD_7\n",
       "19    1.697906   1.353203    1.369027    RAD_8\n",
       "14    1.631360   1.235936    1.276021    RAD_3\n",
       "2     0.422357   0.621810    0.517086    INDUS\n",
       "6    -0.024262  -0.055280   -0.048454      AGE\n",
       "15   -0.000000  -0.391399   -0.389052    RAD_4\n",
       "16   -0.126658  -0.518265   -0.503234    RAD_5\n",
       "13   -1.365961  -1.843667   -1.756881    RAD_2\n",
       "17   -2.113486  -2.508106   -2.495217    RAD_6\n",
       "12   -3.236791  -3.667536   -3.648724    RAD_1\n",
       "8    -4.872897  -5.109656   -4.958641      TAX\n",
       "4    -6.632717  -6.819892   -6.565940      NOX\n",
       "9    -8.400752  -8.455428   -8.431607  PTRATIO\n",
       "0    -9.708141  -9.984341   -9.567877     CRIM\n",
       "7   -17.122870 -17.381223  -16.868997      DIS\n",
       "11  -21.473748 -21.450356  -21.357121    LSTAT"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T)), \"coef_lasso\":list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用最小二乘依赖于模型各项的相互独立，若各项存在相关，则可能产生奇异矩阵，这让最小二乘对随机误差很敏感，所以这时候需要使用岭回归，X本事各项相关，为非满秩矩阵。对于高维数据，使用lasso回归降维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
